package cn.chenxi.hadoopproject.weatherData.service.impl;

import cn.chenxi.hadoopproject.weatherData.mapper.WeatherDataMapper;
import cn.chenxi.hadoopproject.weatherData.pojo.dto.WeatherDataQuery;
import cn.chenxi.hadoopproject.weatherData.pojo.entity.WeatherData;
import cn.chenxi.hadoopproject.weatherData.pojo.vo.WeatherDataVO;
import cn.chenxi.hadoopproject.weatherData.service.WeatherDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class WeatherDataServiceImpl implements WeatherDataService {
    @Autowired
    private WeatherDataMapper weatherDataMapper;

    @Override
    public List<WeatherDataVO> select(WeatherDataQuery weatherDataQuery) {
        log.debug("查询每日天气信息，查询条件为：{} + {} + {}", weatherDataQuery.getCityName(), weatherDataQuery.getYear(), weatherDataQuery.getMonth());

        if (Integer.parseInt(weatherDataQuery.getMonth()) < 10) {
            weatherDataQuery.setMonth("0" + weatherDataQuery.getMonth());
        }
        List<WeatherData> weatherDataList = weatherDataMapper.select(weatherDataQuery);
        List<WeatherDataVO> weatherDataVOS = new ArrayList<>();
        for (WeatherData weatherData : weatherDataList) {
            int day = Integer.parseInt(weatherData.getDate().substring(8, 10));
            float maxTemp = weatherData.getMaxTemp();
            float minTemp = weatherData.getMinTemp();

            WeatherDataVO weatherDataVO = new WeatherDataVO();
            weatherDataVO.setDay(day);
            weatherDataVO.setMaxTemp(maxTemp);
            weatherDataVO.setMinTemp(minTemp);
            weatherDataVOS.add(weatherDataVO);
        }
        return weatherDataVOS;
    }
}
